package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.base.BaseDao;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.DictDao;
import com.atguigu.dao.HouseDao;
import com.atguigu.entity.House;
import com.atguigu.service.HouseService;
import com.atguigu.vo.HouseQueryVo;
import com.atguigu.vo.HouseVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Service(interfaceClass = HouseService.class)
@Transactional
public class HouseServiceImpl extends BaseServiceImpl<House> implements HouseService {
    @Autowired
    private HouseDao houseDao;
    @Override
    protected BaseDao<House> getEntityDao() {
        return houseDao;
    }


    @Override
    public void publish(Long houseId, Integer status) {
        House house = new House();
        house.setId(houseId);
        house.setStatus(status);
        this.houseDao.update(house);
    }
    @Autowired
    private DictDao dictDao;
    @Override
    public House getById(Serializable id) {
        House house = this.houseDao.getById(id);
        //户型：（字典id）
       String houseTypeName = this.dictDao.getNameById(house.getHouseTypeId());
        house.setHouseTypeName(houseTypeName);
        //楼层（字典id）
        String floorName = this.dictDao.getNameById(house.getFloorId());
        house.setFloorName(floorName);
        //建筑结构：（字典id）
        String buildStructureName = this.dictDao.getNameById(house.getBuildStructureId());
        house.setBuildStructureName(buildStructureName);
        //朝向：（字典id）
        String directionName = this.dictDao.getNameById(house.getDirectionId());
        house.setDirectionName(directionName);
        //装修情况：（字典id）
        String decorationName = this.dictDao.getNameById(house.getDecorationId());
        house.setDecorationName(decorationName);
        //房屋用途：（字典id）
        String houseUseName = this.dictDao.getNameById(house.getHouseUseId());
        house.setHouseUseName(houseUseName);
        return house;
    }

    @Override
    public PageInfo<HouseVo> findListPage(int pageNum, int pageSize, HouseQueryVo houseQueryVo) {
        //启用分页插件
        PageHelper.startPage(pageNum,pageSize);
        //查询当前页的数据
        Page<HouseVo> page = this.houseDao.findListPage(houseQueryVo);
        List<HouseVo> houseVoList = page.getResult();
        for(HouseVo houseVo:houseVoList){
            //户型：（字典id）
            String houseTypeName = this.dictDao.getNameById(houseVo.getHouseTypeId());
            houseVo.setHouseTypeName(houseTypeName);
            //楼层（字典id）
            String floorName = this.dictDao.getNameById(houseVo.getFloorId());
            houseVo.setFloorName(floorName);
            //朝向：（字典id）
            String directionName = this.dictDao.getNameById(houseVo.getDirectionId());
            houseVo.setDirectionName(directionName);
        }

        return new PageInfo<>(page);
    }
}
